<%@page import="java.sql.ResultSet" import="java.sql.Statement" import="java.sql.Connection" import="connection.DBConnection"%>
<%@ page import="java.util.*" language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="java.text.SimpleDateFormat" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Payment Voucher</title>
<script language="javascript" type="text/javascript">

function checkDate()
{
	var c1;
	
  	var datevalid=/^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
  		
        c1= document.f14.rdate.value;

        if(c1.length==0)
        {
        	alert("Enter Date");
            return false;
      	 }
        if(!c1.match(datevalid))
        {
            alert("Please Enter Date in DD/MM/YYYY Format");
            document.f14.rdate.value="";
            document.f14.rdate.focus();
            
	         return false;
    	 }
        else
        {
        	dateval=c1.match(datevalid);
        }
        
        var dtYear = dateval[1];        
        dtMonth = dateval[3];
        dtDay=  dateval[5];

     if (dtMonth < 1 || dtMonth > 12) 
        return false;
     else if (dtDay < 1 || dtDay> 31) 
       return false;
     else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31) 
       return false;
     else if (dtMonth == 2){ 
       var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
       if (dtDay> 29 || (dtDay ==29 && !isleap)) 
      	 
      	 alert("Date format is not correct");
       document.f14.rdate.value="";
       document.f14.rdate.focus();
              return false;
        
        return true;
}
     
</script>

 <script language="javascript" type="text/javascript">
 	function addRow(tableID)
	{
 		
 		//var div = document.createElement("div");
 		
		var table=document.getElementById(tableID); //table id
		var rowCount=table.rows.length;  //row count of table
		var row=table.insertRow(rowCount-3); 
		//var row1=table.insertRow(rowCount-2); 
		var colCount=table.rows[0].cells.length; // column count
		
        var cell1 = row.insertCell(0);
        var label1 = document.createElement("label");
        label1.setAttribute("for", select1);
        label1.innerHTML ="Name of Account";
        //element1.value = "Account Name";
        //element1.type = "label";
        cell1.appendChild(label1);
        
        var cell2 = row.insertCell(1);
        var select1 = document.createElement("select");
        var option1 = document.createElement("option");
        select1.name = "account2";
        
        
       for (int k=0; k<=acname.size();k++)
       {
		   // option1 = new Option (acname.get(k), acname.get(k)); 
           // select1.options[select1.options.length] = option1;
           
           option1.add(acname(k).toString());
           select1.options.add(option1);
       
 		}    

       // element2.type = "text";
        cell2.appendChild(select1);

        var cell3 = row.insertCell(2);
        var label2 = document.createElement("input");
        label2.setAttribute("for", element3);
        label2.innerHTML ="Amount";
       // element3.value = "Amount";
       // element3.type = "label";
        cell3.appendChild(label2);

        var cell4 = row.insertCell(3);
        var element4 = document.createElement("input");
        element4.name = "amount2";
        element4.type = "text";
        cell4.appendChild(element4);

        
/*		for(var i=0;i<colCount;i++)
		{
			var newcell=row.insertCell(i);
			
			 newcell.setAttribute("id", "dynamic1");
			 newcell.className = "priyanka";
			// newcell.style.display = "none";
			
			newcell.innerHTML=table.rows[0].cells[i].innerHTML;
			
		//	newcell.childNodes[0].value="";
		//	newcell.childNodes[0].selectedIndex=0;
			
			switch(newcell.childNodes[0].name)
			{
				case"account2":newcell.childNodes[0].value="";
					break;
				case"account2-list":newcell.childNodes[0].selectedIndex=0;
					break;
				case"amount2":newcell.childNodes[0].value="";
					break;
				case"amount2-text":newcell.childNodes[0].value="";
			}
		}
*/
	}
 </script>
 
 <script language="javascript" type="text/javascript">
 function Display1()
 {
	var accountname= document.getElementByName("account2-list");
	var amount= ddocument.getElementByName("amount2-text");
	
	alert("Account name is "+accountname+"  "+amount);
 }
 </script>
 
<%-- <script language="javascript" type="text/javascript">
 
 function createDiv() 
 {
	 /*   var div = document.createElement("div");
	   div.setAttribute("id", "ddiv");
	    div.className = "mydiv";
	    div.style.display = "none";
	    document.body.appendChild(div);
	    */
	/*    var iDiv = document.createElement('div');
	    iDiv.id = 'block';
	    iDiv.className = 'block';
	    document.getElementsByTagName('body')[0].appendChild(iDiv);

	    iDiv.innerHTML = "I'm the first div";

	    // Now create and append to iDiv
	    var innerDiv = document.createElement('div');
	    innerDiv.className = 'block-2';

	    // The variable iDiv is still good... Just append to it.
	    iDiv.appendChild(innerDiv);
	    innerDiv.innerHTML = "I'm the inner div2";
    */
}
 </script>
--%>

</head>
<body>

<%-- 
<%
		List li1 = new ArrayList();
		li1 = (List) session.getAttribute("alldata");
		Iterator it = li1.listIterator();
		
%>

<%
		List li2 = new ArrayList();
		li1 = (List) session.getAttribute("alldata");
		Iterator it1 = li2.listIterator();
		
%>
--%>

<form action="PaymentVoucherServlet" ONSUBMIT="checkDate()">
<center> <h2> Payment Voucher</h2></center>
<table border=0 align="center">

<tr>
	<td> Name of the Bank </td>
	<!-- <td> <input type="text" name="pybankname"> </td> -->
			<!--  pybankname --> 
		<%-- <%  for(int i=0; i<li2.size();) 
			{	%>
			<select>
				<option> <%= li2.get(i++) %> </option>
			</select>
			
		<%	} %>
		--%>
			<%	 DBConnection dbc=new DBConnection();	
				 Connection con=dbc.getNewConnection();
			
				 Statement st = null;
				 ResultSet rs = null;
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select bname from BankMaster"); %>
	 <td> 
	  <select name="pybankname">
	  		<option> </option>
       		 <%  while(rs.next()){ %>
            		<option><%= rs.getString(1)%></option>
      
          <% } %>
       </select>
     </td>
  
	 <% }
		catch(Exception e){} %>
		<td colspan=2> <a href="BankMaster.jsp"> Create New Bank </a> </td>		
</tr>

<tr>
		<td> Bank Date </td>
		<td> <input type="text" name="bankdate">
</tr>

<%-- <tr>
	<td colspan=2> Create New Account  </td>
</tr>
<tr>	
	<td colspan=2> <input type="radio" name="r1"> <a href="BankMaster.jsp"> Bank </a>
	</td>
</tr>
--%>
<tr>
	<td> Payment Entry Number </td>
	
			<%	
			 	String code2="PY1";
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select pyenum from PaymentVoucher2"); %>
       		   
       		 <%  while(rs.next()) 
       		 	{ 
       				String code1=rs.getString(1);
       				
       			   String[] part = code1.split("(?<=\\D)(?=\\d)");
	   		       
	   		       String p0=part[0];		       
	   		       int p1=Integer.parseInt(part[1]);
	   		      
	   		       p1++;
	   		       
	   		       code2=p0+""+p1;

            	} %>
  
	 		<% }
		   catch(Exception e){} %>
	
	<td> <input type="text" name="pyentrynumber" value="<%=code2 %>"> </td>
	<td> Date </td>
	<% SimpleDateFormat current = new SimpleDateFormat("dd/MM/yyyy"); %>	
	<td><input type="text" name="pydate" value="<%=current.format(new Date()) %>" /></td>
</tr>

<tr>

</tr>

<tr>
	<td> Cheque Number </td>
	<td> <input type="text" name="pychecquenumber"> </td>
</tr>
<tr>
	<td colspan=2> Create New Account  </td>
</tr>
<tr>	
	<td colspan=2> <input type="radio" name="r1"> <a href="CustomerMaster.jsp"> Customer </a>
		 <input type="radio" name="r1"> <a href="SupplierMaster.jsp"> Supplier </a> 
		 <input type="radio" name="r1"> <a href="GeneralLedger.jsp"> GeneralLedger </a>
	</td>
</tr>

</table>

<table name="paymenttable" id="paymenttable" align="center"> 

<% ArrayList<String> acname=new ArrayList<String>(); %>
<tr>
	<td> Name of Account</td>
<!-- 	<td> <input type="text" name="pyaccount"> </td> -->
			<!--  pyaccount -->
			 
			 <%	
			 try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select ccode,cname from CustomerMaster"); %>
	<td> 
	  <select name="pyaccount" id="pyaccount" >
		  <option> </option>
       		 <%  while(rs.next()){ %>
            		<option value="<%=rs.getString(1) %>" > <%String arc=rs.getString(2);%> <%=arc %></option>
            		            		
            		            		<% acname.add(arc); %>
        	<% } %>
  				
	 <% }
		catch(Exception e){} %>
	
		
	<%
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select scode,sname from SupplierMaster"); %>
	 	
        	<%  while(rs.next()){ %>
         		   <option value="<%=rs.getString(1) %>"><% String ars= rs.getString(2); %> <%=ars %></option>
         		 	
         		 	<% acname.add(ars); %>
         		 
        	<% } %>
 
	 <% }
		catch(Exception e){} %>
	
	<%
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select gcode,gname from GeneralLedger2"); %>
	 	
        	<%  while(rs.next()){ %>
         		   <option value="<%=rs.getString(1) %>"><% String arg= rs.getString(2);%> <%=arg %></option>
         		   
         		   <% acname.add(arg); %>
         		   
        	<% } %>
        </select>	
	</td>
	 <% }
		catch(Exception e){} %>
			
	<td> Amount </td>
	<td> <input type="text" name="pyamount"> </td>
</tr>


<tr>

</tr>

<tr>
	<td> <input type="button" value="add" onclick="addRow('paymenttable')"> </td> 
	<%-- <td> <input type="button" value="add" onclick="creteDiv()"> </td> --%>
	<td> <input type="button" value="remove" > </td>
</tr>

<%--
<tr>
	<td colspan=2> Create New Account  </td>
</tr>
<tr>	
	<td colspan=2> <input type="radio" name="r1"> <a href="CustomerMaster.jsp"> Customer </a>
		 <input type="radio" name="r1"> <a href="SupplierMaster.jsp"> Supplier </a> 
		 <input type="radio" name="r1"> <a href="GeneralLedger.jsp"> GeneralLedger </a>
	</td>
</tr>
--%>

<%-- <div  visibility:none>

<tr>
	<td> Name of Account </td> --%>
<!-- <td> <input type="text" name="pyaccount2"> </td> -->
	
			<!--  pyaccount2 -->
			 
		<%-- <%  for(int i=0; i<li1.size();) 
				{	%>
				<select>
					<option> <%= li1.get(i++) %> </option>
				</select>
			
		<%	} %>
		
		--%>	
			
<%--			 <%	
			 try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select ccode,cname from CustomerMaster"); %>
	<td> 
	  <select name="pyaccount2" id="pyaccount2" >
	  		<option> </option>
       		 <%  while(rs.next()){ %>
            		<option value="<%=rs.getString(1) %>" > <%=rs.getString(2)%> </option>
            		            		
        	<% } %>
  				
	 <% }
		catch(Exception e){} %>
	
		
	<%
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select scode,sname from SupplierMaster"); %>
	 	
        	<%  while(rs.next()){ %>
         		   <option value="<%=rs.getString(1) %>"><%= rs.getString(2)%></option>
         		 
        	<% } %>
 
	 <% }
		catch(Exception e){} %>
	
	<%
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select gcode,gname from GeneralLedger2"); %>
	 	
        	<%  while(rs.next()){ %>
         		   <option value="<%=rs.getString(1) %>"><%= rs.getString(2)%></option>
         		   
        	<% } %>
        </select>	
	</td>
	 <% }
		catch(Exception e){} %>
</tr>

<tr>
	<td> </td>
	<td> </td>
</tr>

<tr>
	<td> Amount </td>
	<td> <input type="text" name="pyamount2"> </td>
</tr>
 </div>
--%>
<tr>
	<td> </td>
	<td> </td>
</tr>

<tr>
	<td> Narration </td>
	<td> <input type="text" name="pynarration"> </td>
</tr>

<tr>
	<td> <input type="button" value="check" onClick="Display1()">
</tr>
<tr>
	<td> <input type="submit" value="submit"> </td>  
</tr>


</table>
</form>
</body>
</html>